import http from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputValue:'',
        sensuccesIshow:false,
        myupload:'',
        myemail:'',
        id:'',
        emailFasle:false,
        isShow:false,
        markison:'',
        zylink:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({myupload:options.myupload?options.myupload:'',id:options.itemid,markison:options.mark?options.mark:'',zylink:options.url?options.url:''})
        this.getMyinfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //获取邮箱
    bindKeyInput: function (e) {
        // var reg = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$');       
        // var emailVar = reg.test(e.detail.value);     // 得到的值为布尔型
        // if(!emailVar){

        // }
        this.setData({
          inputValue: e.detail.value
        })
    },
    async sendemail(){

        let {inputValue,myemail} = this.data
        let that = this
        var reg = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$');       
        var emailVar = reg.test(inputValue);     // 得到的值为布尔型
        if(!emailVar){
            wx.showToast({
                title: '请输入有效的邮箱',
                icon: 'error',
                duration: 2000
            })
            return false
        }
        this.setData({emailFasle:true})
        //判断邮箱是否一样，不一样修改
        if(inputValue!=myemail){
            let options={
                url:'/setMyEmail',
                method:'POST',
                data:{Email:inputValue,userId:JSON.parse(wx.getStorageSync('userInfo')).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
            let res = await http(options)
            if(res.data.code==5){
                this.setData({isShow:true})
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }
        // if(that.data.myupload==1){
        //     let options={
        //         url:'/getProgramDownloadNumId2',
        //         method:'POST',
        //         data:{itemcaseId:that.data.id,userId:JSON.parse(wx.getStorageSync('userInfo')).id},
        //         header:{"content-type":"application/json;charset=UTF-8"}
        //     }
        //     let res = await http(options)
        //     if(res.data.code==0){
        //         this.setData({sensuccesIshow:true,emailFasle:false})
        //         setTimeout(()=>{
        //             this.setData({sensuccesIshow:false})
        //             wx.navigateBack({
        //                 delta:1 //返回上一级页面
        //             })
        //         },2000)
        //     }
        // }else{
            if(that.data.markison=='中秋'){
                console.info('sss')
                let options={
                    url:'/getProgramDownloadNumId3',
                    method:'POST',
                    data:{Email:inputValue,url:that.data.zylink,userId:JSON.parse(wx.getStorageSync('userInfo')).id},
                    header:{"content-type":"application/json;charset=UTF-8"}
                }
                let res = await http(options)
                if(res.data.code==0){
                    this.setData({sensuccesIshow:true,emailFasle:false})
                    setTimeout(()=>{
                        this.setData({sensuccesIshow:false})
                        wx.navigateBack({
                            delta:1 //返回上一级页面
                        })
                    },2000)
                }else{
                    if(res.data.code==5){
                        this.setData({isShow:true})
                    }else{
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'error',
                            duration: 2000
                        })
                    }
                }
            }else{
                let options={
                    url:'/getProgramDownloadNumId',
                    method:'POST',
                    data:{itemcaseId:that.data.id,userId:JSON.parse(wx.getStorageSync('userInfo')).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
                    header:{"content-type":"application/json;charset=UTF-8"}
                }
                let res = await http(options)
                if(res.data.code==0){
                    this.setData({sensuccesIshow:true,emailFasle:false})
                    setTimeout(()=>{
                        this.setData({sensuccesIshow:false})
                        wx.navigateBack({
                            delta:1 //返回上一级页面
                        })
                    },2000)
                }else{
                    if(res.data.code==5){
                        this.setData({isShow:true})
                    }else{
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'error',
                            duration: 2000
                        })
                    }
                }
            }
        
        // }
            // console.info(res)
            // this.setData({sensuccesIshow:true})
            // setTimeout(()=>{
            //     this.setData({sensuccesIshow:false})
            // },2000)
    },
    // 获取个人信息接口
    async getMyinfo(){
        let that = this
        let options={
            url:'/getMyEmail',
            method:'POST',
            data:{userId:JSON.parse(wx.getStorageSync('userInfo')).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        if(res.data.code==0){
            this.setData({myemail:res.data.obj,inputValue:res.data.obj})
        } else{
            if(res.data.code==5){
                this.setData({isShow:true})
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }
    }
})